package pms.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

/** 
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
 * @author josef@dr-schneeberger.de
 */
public class SavePerson extends AllePersonen {
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		httpServletRequest.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(httpServletRequest.getParameter("id"));
		String vorname = httpServletRequest.getParameter("vorname");
		String nachname = httpServletRequest.getParameter("nachname");
		String telefon = httpServletRequest.getParameter("telefon");
		String abteilung = httpServletRequest.getParameter("abteilung");
//		int pr = Integer.parseInt(httpServletRequest.getParameter("present"));
		int pr = 1 ;
		
		int hr=4; //= Integer.parseInt(httpServletRequest.getParameter("hr"));
		Date ar = new Date();
		//pr = 99;
		System.out.print("gefunden Y ");//hbi
		//String ImagePath = System.getProperty("user.dir") + "\\WebContent\\images\\dbImages\\SeriesImage";
//		String ImagePath = "C:\\Studium\\tk\\FH\\Semester 7\\Progammierung Multimedialer Systeme\\Project\\PMS\\hbi\\WebContent\\images\\dbImages\\SeriesImage";
		String ImagePath = "C:\\workspace\\hbi\\WebContent\\images\\dbImages\\SeriesImage";

        
		File file1 = new File(ImagePath + "/image" + 0 + ".jpg");
		byte[] bFile1 = new byte[(int) file1.length()];
        try {
            //convert file into array of bytes
   	     FileInputStream fileInputStream1 = new FileInputStream(file1); 
   	     fileInputStream1.read(bFile1); 
   	     fileInputStream1.close();}
	     catch (Exception e) {
		     e.printStackTrace();
	    }
		
		
		
		if (id == 0) {
			getPersonDao().create(vorname, nachname, telefon, abteilung, ar, pr, 1 ,bFile1, bFile1, bFile1, bFile1,bFile1,
					bFile1,bFile1,bFile1,bFile1,bFile1,bFile1,bFile1,bFile1,bFile1,bFile1,bFile1);
	     } else {
			getPersonDao().modify(id, vorname, nachname, telefon, abteilung, ar, pr, hr);
		}
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Bild aufnehmen");
		mv.addObject("message", "Bild vorschau");
//		mv.addObject("personen", personDao.findAll());
//		mv.addObject("abteilung", "raum");
//		mv.addObject("present", "alle Anwesenden");
		mv.setViewName("capture-pic");
		return mv ;
//		return super.handleRequestInternal(httpServletRequest, httpServletResponse);
	}

}
